//
//  QDPaymentUntility.m
//  QDMetroClient
//
//  Created by YueWen on 2019/3/6.
//  Copyright © 2019 YueWen. All rights reserved.
//

#import "QDPaymentUntility.h"

/// 中建投源码

@implementation QDPaymentUntility

+ (NSString *)wxpaySigning:(NSString *)message {
    
    NSDictionary * temp                = [NSJSONSerialization JSONObjectWithData:[message dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingAllowFragments error:nil];
    
    NSString *appid                    = temp[@"appid"];
    NSString *mchid                    = temp[@"mch_id"];
    NSString *planId                   = temp[@"plan_id"];
    NSString *contract_code            = temp[@"contract_code"];
    NSString *request_serial           = temp[@"request_serial"];
    NSString *contract_display_account = temp[@"contract_display_account"];
    //    NSString *notifyurl                = [temp[@"notify_url"] URLEncodedString].URLEncodedString;
    
    NSString *notifyurl = [temp[@"notify_url"] stringByAddingPercentEncodingWithAllowedCharacters: NSCharacterSet.URLUserAllowedCharacterSet];
    notifyurl = [notifyurl stringByAddingPercentEncodingWithAllowedCharacters: NSCharacterSet.URLUserAllowedCharacterSet];// ok
    
    NSString *version                  = temp[@"version"];
    NSString *sign                     = temp[@"sign"];
    NSString *timestamp                = temp[@"timestamp"];
    NSString *returnApp                = temp[@"return_app"];
    return [NSString stringWithFormat:@"%@?appid=%@&mch_id=%@&plan_id=%@&contract_code=%@&request_serial=%@&contract_display_account=%@&notify_url=%@&version=%@&sign=%@&timestamp=%@&return_app=%@",@"https://api.mch.weixin.qq.com/papay/entrustweb",appid,mchid,planId,contract_code,request_serial,contract_display_account,notifyurl,version,sign,timestamp,returnApp];
}


+ (PayReq *)wxPaying:(NSString *)message {
    
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:[message dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingAllowFragments error:nil];
    
    PayReq *request   = [[PayReq alloc]init];
    request.nonceStr  = dic[@"noncestr"];
    request.package   = @"Sign=WXPay";
    request.partnerId = dic[@"partnerid"]; // 商户号
    request.prepayId  = dic[@"prepayid"];   // 预支付订单
    request.timeStamp = [dic[@"timestamp"] intValue];   // 时间戳
    request.sign      = dic[@"sign"];
    
    return request;
}

@end
